<?php
namespace App\Services\Main;

use App\Repositories\Admin\CompanyRepository;
use App\Repositories\Admin\UserRepository;
use App\Repositories\Main\MsgDetailRepository;
use App\Repositories\Main\MsgTalkRepository;

class MsgTalkService
{
    private $service;
    private $detail;
    private $user;
    private $com;

    public function __construct()
    {
        $this->service = new MsgTalkRepository();
        $this->detail = new MsgDetailRepository();
        $this->user = new UserRepository();
        $this->com = new CompanyRepository();
    }

    public function getIndex()
    {
        return $this->service->getIndexList();
    }

    public function setAdd($data)
    {
        return $this->service->setAdd($data);
    }

    /**
     * 新消息提示
     */
    public function getNewTips($uid)
    {
        return array(
            'business' => $this->service->getNewTips($uid,1),
            'job' => $this->service->getNewTips($uid,2),
            'sys' => $this->service->getNewTips($uid,3),
            'inside' => $this->service->getNewTips($uid,4),
            'reward' => $this->service->getNewTips($uid,5),
        );
    }

    /**
     * 商务消息
     */
    public function getComTalks($uid)
    {
        $models = $this->service->getTalks($uid,1);
        if (!count($models)) { return ''; }
        $dataArr = array();
        foreach ($models as $k=>$model) {
            if ($model->own==$uid) {
                $new = $model->own_new;
                $company = $this->com->getShowOneByUid($model->other);
            } else {
                $new = $model->other_new;
                $company = $this->com->getShowOneByUid($uid);
            }
            $dataArr[$k]['id'] = $model->id;
            $dataArr[$k]['new'] = $new?$new:0;
            $dataArr[$k]['comName'] = $company?$company->com_name:'测试公司';
            $dataArr[$k]['lastTime'] = date('Y-m-d',$model->updated_at);
        }
        return $dataArr;
    }

    /**
     * 消息内容窗口
     */
    public function getDetails($uid,$talkId)
    {
        $talk = $this->service->getShowOne($talkId);
        if (!$talk) { return ''; }
        if ($talk->own==$uid) {
            $from = 1;
            $models = $this->detail->getIndexByTalk($talkId,2);
        } else {
            $from = 2;
            $models = $this->detail->getIndexByTalk($talkId,1);
        }
        if (!count($models)) { return ''; }
        $dataArr = array();
        $detailIds = array();
        foreach ($models as $k=>$model) {
            $dataArr[$k]['id'] = $model->id;
            $dataArr[$k]['detail'] = $model->detail;
            $detailIds[] = $model->id;
        }
        //更新消息状态
        $this->detail->setStatus($detailIds);
        $this->service->setLast($talk,$from);
        return $dataArr;
    }

    /**
     * 消息内容新增
     */
    public function setSend($uid,$talkId,$detail)
    {
        $talk = $this->service->getShowOne($talkId);
        if (!$talk) { return ''; }
        if ($talk->own==$uid) {
            $from = 1;
        } else {
            $from = 2;
        }
        $lastId = $this->detail->setAdd($talkId,$from,$detail);
        $this->service->setModify($talk,$from);
        return $lastId;
    }

    /**
     * 招聘消息会话列表
     */
    public function getJobTalks($uid,$type)
    {
        $models = $this->service->getTalks($uid,2);
        if (!count($models)) { return ''; }
        $dataArr = array();
        foreach ($models as $k=>$model) {
            if ($model->own==$uid) {
                $new = $model->own_new;
                if ($type==1) { //当前是个人
                    $company = $this->com->getShowOneByUid($model->other);
                    $otherName = $company?$company->com_name:'测试公司';
                    $own = $uid;
                    $other = $model->other;
                } else { //当前是公司
                    $user = $this->user->getShowOne($model->other);
                    $otherName = $user?$user->name:'测试个人';
                    $own = $model->other;
                    $other = $uid;
                }
            } else {
                $new = $model->other_new;
                if ($type==1) { //当前是个人
                    $company = $this->com->getShowOneByUid($uid);
                    $otherName = $company?$company->com_name:'测试公司';
                    $own = $model->other;
                    $other = $uid;
                } else { //当前是公司
                    $user = $this->user->getShowOne($uid);
                    $otherName = $user?$user->name:'测试个人';
                    $own = $uid;
                    $other = $model->other;
                }
            }
            $dataArr[$k]['id'] = $model->id;
            $dataArr[$k]['new'] = $new?$new:0;
            $dataArr[$k]['own'] = $own;
            $dataArr[$k]['other'] = $other;
            $dataArr[$k]['otherName'] = $otherName;
            $dataArr[$k]['jobId'] = 0;
            $dataArr[$k]['lastTime'] = date('Y-m-d',$model->updated_at);
        }
        return $dataArr;
    }

//    public function modify($id)
//    {
//        return $this->service->setLastTalk($id);
//    }

    public function getOne($id)
    {
        return $this->service->getShowOne($id);
    }

    public function getAppJobShow($uid,$uid2)
    {
        return $this->service->getAppJobShow($uid,$uid2);
    }
}